package cn.com.jinhao.provider.business.config;

/**
 * hotline-spring-cloud
 * <p>Description: Swagger2相关信息配置</p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2019/12/23
 * @since 2019-12-23
 */

import com.google.common.base.Predicates;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
@Data
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfig {
    private Boolean enabled;
    private String host;
    private String path;
    private String groupName;
    private String title;
    private String desc;
    private String version;
    private String license;
    private String licenseUrl;
    private String basePackage;
    private String contactName;
    private String contactUrl;
    private String contactEmail;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .enable(enabled)
            .apiInfo(apiInfo())
            .groupName(groupName)
            .host(host)
            .pathMapping(path)
            .select()
            .apis(RequestHandlerSelectors.basePackage(basePackage))
            .paths(Predicates.not(PathSelectors.regex("/error.*")))
            .paths(Predicates.not(PathSelectors.regex("/actuator.*")))
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title(title)
            .license(license)
            .description(desc)
            .contact(new Contact(contactName, contactUrl, contactEmail))
            .licenseUrl(licenseUrl)
            .version(version)
            .build();
    }
}
